<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="点位名称" prop="nodeName">
        <el-input
          v-model="queryParams.nodeName"
          placeholder="请输入点位名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区域搜索" prop="regionId">
        <!-- <el-input v-model="queryParams.regionId" placeholder="请输入区域ID" clearable @keyup.enter="handleQuery" /> -->
        <el-select v-model="queryParams.regionId" placeholder="请选择区域" clearable>
            <el-option v-for="item in regionList" :key="item.id" :label="item.regionName" :value="item.id"></el-option>

        </el-select>

    </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:node:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:node:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:node:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:node:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="nodeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id" align="center" prop="id" />
      <el-table-column label="点位名称" align="center" prop="nodeName" />
      <el-table-column label="详细地址" align="center" prop="address" />
      <el-table-column label="商圈类型" align="center" prop="businessType" >
        <template #default="scope">
      <dict-tag :options="business_type" :value="scope.row.businessType" />
    </template>
      </el-table-column>
      <el-table-column label="区域名称" align="center" prop="qyname" />
      <el-table-column label="合作商" align="center" prop="hzsname" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" @click="getNodeInfo(scope.row)" v-hasPermi="['manage:vm:list']">查看详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:node:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:node:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog title="点位详情" v-model="open1" width="500px" append-to-body>
            <el-table :data="vmList">
                <el-table-column label="序号" type="index" width="80" align="center" prop="id" />
                <el-table-column label="设备编号" align="center" prop="innerCode" />
                <el-table-column label="设备状态" align="center" prop="vmStatus">
                    <template #default="scope">
                        <dict-tag :options="vm_status" :value="scope.row.vmStatus" />
                    </template>

                </el-table-column>

                <el-table-column label="最后一次供货时间" align="center" prop="lastSupplyTime" width="180">
                    <template #default="scope">
                        <span>{{ parseTime(scope.row.lastSupplyTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>

                    </template>

                </el-table-column>

            </el-table>

        </el-dialog>

<!-- 添加或修改点位管理对话框 -->
<el-dialog :title="title" v-model="open" width="500px" append-to-body>
  <el-form ref="nodeRef" :model="form" :rules="rules" label-width="100px">
    <el-form-item label="点位名称" prop="nodeName">
      <el-input v-model="form.nodeName" placeholder="请输入点位名称" />
    </el-form-item>

    <el-form-item label="所在区域" prop="regionId">
      <!-- <el-input v-model="form.regionId" placeholder="请输入区域ID" /> -->
        <el-select v-model="form.regionId" placeholder="请选择区域" clearable>
        <el-option v-for="item in regionList" :key="item.id" :label="item.regionName" :value="item.id"></el-option>

      </el-select>

    </el-form-item>

    <el-form-item label="商圈类型" prop="businessType">
      <el-select v-model="form.businessType" placeholder="请选择商圈类型">
        <el-option v-for="dict in  dictlist" :key="dict.dictCode" :label="dict.dictLabel"
          :value="parseInt(dict.dictCode)"></el-option>

      </el-select>

    </el-form-item>

    <el-form-item label="归属合作商" prop="partnerId">
      <!-- <el-input v-model="form.partnerId" placeholder="请输入合作商ID" /> -->
        <el-select v-model="form.partnerId" placeholder="请选择合作商" clearable>
        <el-option v-for="item in partnerList" :key="item.id" :label="item.partnerName" :value="item.id"></el-option>

      </el-select>

    </el-form-item>

    <el-form-item label="详细地址" prop="address">
      <el-input type="textarea" v-model="form.address" placeholder="请输入详细地址" />
    </el-form-item>

  </el-form>

  <template #footer>
    <div class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>

      <el-button @click="cancel">取 消</el-button>

    </div>

  </template>

</el-dialog>
  </div>
</template>
<script setup>
const { proxy } = getCurrentInstance();
const { business_type } = proxy.useDict("business_type");
const { vm_status } = proxy.useDict("vm_status");

</script>
<script>
import { listNode, getNode, delNode, addNode, updateNode } from "@/api/system/node";
import { listMachine, getMachine, delMachine, addMachine, updateMachine } from "@/api/system/machine";
import {getDicts} from "@/api/system/dict/data"
import { listPartner } from "@/api/system/partner";
import { loadAllParams } from "@/api/page"
import { listRegion } from "@/api/system/region";

export default {
  name: "Node",
  data() {
    return {
      vmList:[],
      regionInfoOpen:false,
      regionList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 点位表格数据
      nodeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open1:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nodeName: null,
        address: null,
        businessType: null,
        regionId: null,
        partnerId: null,
      },
      // 表单参数
      form: {},
      partnerList:[],
      dictlist:[],
      // 表单校验
      rules: {
        nodeName: [
          { required: true, message: "点位名称不能为空", trigger: "blur" }
        ],
        address: [
          { required: true, message: "详细地址不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "修改时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
 this.getRegionList()
    this.getPartnerList()
    this.getDictslist()
  },
  methods: {
    getNodeInfo(row){
      console.log(row)

      this.open1=true;
      this.queryParams.pageSize=10000000
      this.queryParams.nodeId=row.id;
      listMachine(this.queryParams).then(res=>{
        console.log(res)
        this.vmList=res.rows
      })
    },
    qyxq(row){
      this.regionInfoOpen=true;

      
    },
    getDictslist(){
      getDicts("business_type").then(res=>{
        console.log(res)
        this.dictlist=res.data
      })
    },
  getRegionList() {
        listRegion(loadAllParams).then(response => {
            this.regionList = response.rows;
        });
    },
     getPartnerList() {
        listPartner(loadAllParams).then(response => {
            this.partnerList = response.rows;
        });
    },
    /** 查询点位列表 */
    getList() {
      this.loading = true;
      listNode(this.queryParams).then(response => {
        this.nodeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        nodeName: null,
        address: null,
        businessType: null,
        regionId: null,
        partnerId: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加点位";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getNode(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改点位";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["nodeRef"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateNode(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNode(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除点位编号为"' + ids + '"的数据项？').then(function() {
        return delNode(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/node/export', {
        ...this.queryParams
      }, `node_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
